/**
 * Created by shenchangmin on 2016/12/12.
 */
const config = require('config-lite');
const Mongolass = require('mongolass');
const mongolass = new Mongolass();
const moment = require('moment');
const objectIdToTimestamp = require('objectid-to-timestamp');
mongolass.plugin('addCreatedAt', {
    afterFind(results) {
        results.forEach((item) => {
            item.created_at = moment(objectIdToTimestamp(item._id)).format('YYYY-MM-DD HH:mm');
        });
    },
    afterFindOne(result) {
        if (result) {
            result.created_at = moment(objectIdToTimestamp(result._id)).format('YYYY-MM-DD HH:mm');
        }
        return result;
    }
});
mongolass.connect(config.mongodb);
exports.User = mongolass.model('User', {
    name: {
        type: 'string'
    },
    password: {
        type: 'string'
    },
    avatar: {
        type: 'string'
    },
    gender: {
        type: 'string',
        enum: ['m', 'f', 'x']
    },
    bio: {
        type: 'string'
    }
});
exports.User.index({
    name: 1
}, {
    unique: true
}).exec(); // 根据用户名找到用户，用户名全局唯一
